import axios from "axios"
import qs from 'qs'
import { Message } from 'element-ui';

// const accessToken = sessionStorage.getItem('accessToken')
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'

let Axios = axios.create({
	// baseURL: '',
	// timeout: 300000,
	headers: {
		// 'token': accessToken ? accessToken : ''
	}
})

// 添加响应拦截器
Axios.interceptors.response.use(function (response) {
	let resp = response.data
	// 对响应数据做点什么  
	if (response.status === 200) {
		// 判断是401重定向
		if (resp.code === 401) {
			if (resp.data.location) {
				window.location.href = location + '?redirect_url=' + encodeURIComponent(window.location.href)
			}
		}
		// else if (resp.code !== 200) {
		// 	// 操作错误
		// 	Message.error(resp.message);
		// 	return Promise.reject(resp.data)
		// }
	}
	return resp
}, function (error) {
	// 抛出错误
	Message.error(error.message)
	return Promise.reject(error)
}
)



/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [query参数（拼在url后面的参数）]
 */
export const get = (url, params) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.get(url, { params }, {
		headers: {
			'token': accessToken ? accessToken : ''
		}
	});
};

/**
 * post方法，对应json传参（设为了默认）
 * @param {String} url [请求的url地址]
 * @param {Object} data [request payload参数]
 */
export const post = (url, data) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.post(url, data, {
		headers: {
			'token': accessToken ? accessToken : ''
		}
	})
};

/**
 * post方法，对应query传参
 * @param {String} url [请求的url地址]
 * @param {Object} params [query参数]
 */
export const postQuery = (url, params) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.post(url, null, {
		params: params,
		headers: {
			'token': accessToken ? accessToken : ''
		}
	})
};

/**
 * post方法，对应query与body传参
 * @param {String} url [请求的url地址]
 * @param {Object} data [request payload参数]
 * @param {Object} params [query参数]
 */
// post方法，对应request payload和query传参
export const postDataAndQuery = (url, data, params) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.post(url, null, {
		data,
		params,
		headers: {
			'token': accessToken ? accessToken : ''
		}
	})
};

/**
 * post方法，对应formdata形式传参
 * @param {String} url [请求的url地址]
 * @param {Object} data [formdata参数]
 */
export const postForm = (url, data) => {
	let qsdata = qs.stringify(data)
	return Axios.post(url, qsdata, {
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
		}
	})
};

/**
 * post方法，获取流文件的传参
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
*/
export const postGetBlob = (url, data) => {
	return Axios.post(url, data, {
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
			'X-Requested-With': 'XMLHttpRequest',
			// 核心时设置responseType
			'responseType': 'blob'
		},
		transformRequest: [function (val) {
			return qs.stringify(val)
		}]
	})
};

/**
 * post方法上传file
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
*/
export const postUpload = (url, data) => {
	const config = {
		headers: {
			'Content-Type': 'multipart/form-data',
		}
	}
	return Axios.post(url, data, config)
};

/**
 * post方法上传多个file，跟后台约定好，以file1、file2等形式传参。
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
*/
export const postUploadMult = (url, data) => {
	let formData = new FormData()
	for (const key in data) {
		if (data[key] !== null && data[key] !== undefined) {
			formData.append(key, data[key])
		}
	}
	return Axios.post(url, formData, {
		headers: {
			'Content-Type': 'multipart/form-data',
		}
	});
};

/**
 * put方法，对应put请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export const put = (url, params) => {
	return Axios.put(url, params, {});
};

export default { get, post, postForm, postQuery, postDataAndQuery, postGetBlob, postUpload, postUploadMult, put, Axios }